package com.rainbow.seata.order.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.rainbow.seata.common.model.Order;
import com.rainbow.seata.common.service.OrderService;
import com.rainbow.seata.common.service.StorageService;
import com.rainbow.seata.order.mapper.OrderMapper;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 订单服务
 * @author rainbow
 */
@Service
@Slf4j
@Component
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Reference
    private StorageService storageService;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public String buy(String goodName) throws RpcException {
        String xid = RootContext.getXID();
        log.info("开始全局事务，XID = " + xid);
        System.out.println("dubbo: " + storageService.getStore("stq"));
        Order order = new Order();
        order.setUserId(""+23);
//        order.setCommodityCode("sdsd:"+goodName);
        order.setOrderNo(goodName);
        orderMapper.insert(order);
//        System.out.println("buy :" + goodName);
        int a = 0;
        if(a>0){
        }else{
            throw new RpcException("rpc-err");
        }
        return "orderService : buy : " + goodName;
    }
}
